package com.company;

import java.util.*;
import java.util.stream.Collectors;

public class Card implements Comparable<Card> {
    public static final int SINGLE = 1;
    public static final int PAIR = 2;
    public static final int TRIPLE = 3;
    public static final int STRAIGHT = 4;
    public static final int STRAIGHT_PAIR = 5;
    public static final int AIRPLANE = 6;
    public static final int BOMB = 7;
    public static final int KING_BOMB = 8;

    private static final String[] SUITS = {"♥", "♦", "♠", "♣"};
    private static final String[] RANKS = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

    private String suit;
    private String rank;

    public Card(String suit, String rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public static List<Card> createDeck() {
        List<Card> deck = new ArrayList<>();
        for (String suit : SUITS) {
            for (String rank : RANKS) {
                deck.add(new Card(suit, rank));
            }
        }
        deck.add(new Card("", "小王"));
        deck.add(new Card("", "大王"));
        return deck;
    }

    public static int getCardType(List<Card> cards) {
        if (cards == null || cards.isEmpty()) return -1;

        int size = cards.size();

        // 王炸
        if (size == 2 && cards.get(0).getRank().equals("小王") && cards.get(1).getRank().equals("大王")) {
            return KING_BOMB;
        }

        // 单张
        if (size == 1) return SINGLE;

        // 对子
        if (size == 2 && cards.get(0).getWeight() == cards.get(1).getWeight()) {
            return PAIR;
        }

        // 三张
        if (size == 3 && cards.get(0).getWeight() == cards.get(1).getWeight()
                && cards.get(1).getWeight() == cards.get(2).getWeight()) {
            return TRIPLE;
        }

        // 炸弹(四张相同)
        if (size == 4 && cards.get(0).getWeight() == cards.get(1).getWeight()
                && cards.get(1).getWeight() == cards.get(2).getWeight()
                && cards.get(2).getWeight() == cards.get(3).getWeight()) {
            return BOMB;
        }

        // 其他牌型判断...

        return -1; // 无效牌型
    }

    public static int getCardsWeight(List<Card> cards) {
        int type = getCardType(cards);
        if (type == -1) return -1;

        if (type == KING_BOMB) return Integer.MAX_VALUE;
        if (type == BOMB) return cards.get(0).getWeight() * 100;

        return cards.get(0).getWeight();
    }

    public static List<Card> parseCards(String cardsData) {
        List<Card> cards = new ArrayList<>();

        // 移除JSON数组的中括号
        cardsData = cardsData.replaceAll("[\\[\\]]", "");

        if (cardsData.isEmpty()) {
            return cards;
        }

        String[] parts = cardsData.split(",");
        for (String part : parts) {
            part = part.trim().replaceAll("\"", "");
            if (part.equals("小王")) {
                cards.add(new Card("", "小王"));
            } else if (part.equals("大王")) {
                cards.add(new Card("", "大王"));
            } else if (part.length() >= 2) {
                String suit = part.substring(0, 1);
                String rank = part.substring(1);
                cards.add(new Card(suit, rank));
            }
        }
        return cards;
    }
    public static List<Card> parseCards(List<String> cardStrings) {
        List<Card> cards = new ArrayList<>();
        for (String cardStr : cardStrings) {
            if (cardStr.equals("小王")) {
                cards.add(new Card("", "小王"));
            } else if (cardStr.equals("大王")) {
                cards.add(new Card("", "大王"));
            } else if (cardStr.length() >= 2) {
                String suit = cardStr.substring(0, 1);
                String rank = cardStr.substring(1);
                cards.add(new Card(suit, rank));
            }
        }
        return cards;
    }

    public static String cardsToJson(List<Card> cards) {
        return cards.stream()
                .map(Card::toString)
                .collect(Collectors.joining(",", "[", "]"));
    }

    public int getWeight() {
        switch (rank) {
            case "3": return 3;
            case "4": return 4;
            case "5": return 5;
            case "6": return 6;
            case "7": return 7;
            case "8": return 8;
            case "9": return 9;
            case "10": return 10;
            case "J": return 11;
            case "Q": return 12;
            case "K": return 13;
            case "A": return 14;
            case "2": return 15;
            case "小王": return 16;
            case "大王": return 17;
            default: return 0;
        }
    }

    @Override
    public String toString() {
        return suit + rank;
    }

    @Override
    public int compareTo(Card other) {
        return Integer.compare(this.getWeight(), other.getWeight());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Card card = (Card) obj;
        return Objects.equals(suit, card.suit) && Objects.equals(rank, card.rank);
    }

    @Override
    public int hashCode() {
        return Objects.hash(suit, rank);
    }

    public String getSuit() { return suit; }
    public String getRank() { return rank; }
}